import { HttpClient, HttpEventType, HttpRequest } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { res } from 'src/common/url.service';
import { finalize, tap, map } from 'rxjs/operators';
@Component({
  selector: 'app-hldzb',
  templateUrl: './hldzb.component.html',
  styleUrls: ['./hldzb.component.less'],
})
export class HLDZBComponent implements OnInit {
  constructor(private http: HttpClient, private message: NzMessageService) {}

  ngOnInit(): void {}
  current = 0;

  fileList = [];

  beforeUploadshp = (file) => {
    this.fileList = this.fileList.concat(file);

    return false;
  };

  next1() {
    if (this.fileList.length <= 0) {
      this.message.error('请添加文件！');
      return;
    }

    const form = new FormData();
    this.fileList.map((file) => form.append('files[]', file));

    this.http
      .post(`http://127.0.0.1:7001/pdf/extractionPoint`, form)
      .subscribe(({ code, data, msg }: any) => {
        if (code == 200) {
          this.current = 1;
          this.stepTwoData = data;
          this.tabs = Object.keys(this.stepTwoData);
        }
      });
  }
  tabs: any = [];
  stepTwoData: any = {};
  selectedIndex = 0;

  next2() {
    this.http
      .post(`http://127.0.0.1:7001/pdf/createHLDZB`, {
        ...this.stepTwoData,
      })
      .subscribe(({ code, data, msg }: any) => {
        if (code == 200) {
          this.current = 2;
          // this.current = 1;
          this.downloadUrl = data;
          // this.tabs = Object.keys(this.stepTwoData);
        }
      });
  }
  downloadUrl = '';
  dwonloadPercent = 0;

  download(evt: any) {
    evt.preventDefault();
    this.http
      .request(
        new HttpRequest('GET', this.downloadUrl, {
          reportProgress: true,
          responseType: 'blob',
        })
      )
      .subscribe((res) => {
        switch (res.type) {
          case HttpEventType.DownloadProgress: {
            this.dwonloadPercent = (res.loaded / res.total) * 100;
            break;
          }
          case HttpEventType.Response: {
            const url = URL.createObjectURL(res.body);
            const a = document.createElement('a');
            a.href = url;
            a.download = '机场航路点.zip';
            a.click();
            this.dwonloadPercent = 0;
          }
        }
      });
  }
}
